package com.by.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.by.entity.Order;
import com.by.entity.Orderdetail;
import com.by.mapper.OrderMapper;
import com.by.service.OrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.by.service.OrderdetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 黄佳辉
 * @since 2023-01-04
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements OrderService {

    @Autowired
    private OrderdetailService orderdetailService;

    @Override
    public boolean saveWithDetail(Order order) {
        boolean saveFlag = this.save(order);
        List<Orderdetail> orderDetail = order.getOrderDetail();
        String orderId = order.getId();
        for (Orderdetail orderdetail : orderDetail) {
            orderdetail.setOid(orderId);
        }
        if (saveFlag) {
            orderdetailService.saveBatch(orderDetail);
        }
        return saveFlag;
    }

    @Override
    public Order getWithDetail(String id) {
        LambdaQueryWrapper<Orderdetail> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Orderdetail::getOid, id);
        List<Orderdetail> orderdetailList = orderdetailService.list(wrapper);
        Order order = this.getById(id);
        order.setOrderDetail(orderdetailList);
        return order;

    }
}
